<template>
  <div>
    <mt-field label="账号" placeholder="账号" type="tel" v-model="S_ID"></mt-field>
    <mt-field label="密码" placeholder="请输入密码" type="password" v-model="password"></mt-field>
    <!--<router-link to="/">-->
      <mt-button type="default" class="set-button1" @click="HandleLogin">登陆</mt-button>
    <!--</router-link>-->
    <mt-button type="default" class="set-button2" @click="btnChange">重置</mt-button>
    <router-link to="/Register">
      <div>忘记密码？</div>
    </router-link>
    <router-link to="/Register">
      <div>新建账户？</div>
    </router-link>
    <!--<div>-->
      <!--<div>{{this.S_ID}}</div>-->
      <!--<div>{{this.password}}</div>-->
      <!--<div v-for="mydata in my">{{mydata.name}}</div>-->
    <!--</div>-->
  </div>
</template>

<script>
  import { Button } from 'mint-ui';
  import { Field } from 'mint-ui';
  import { MessageBox } from 'mint-ui';
  // import router from './router';
  import axios from 'axios';
  import qs from 'qs';

  export default {

    name: "LoginContent",
    props:{
      userMessage:Array
    },
    components:{
      MtField:Field,
      MtButton:Button,
    },
    data:function () {
      return{
        my:[],
        userList:[],
        obItem:{
          ItemPassword:'',
          ItemNumber:''
        },
        password:'',
        S_ID:'',
      }
    },
    methods:{
      btnChange:function () {
        this.password = null;
        this.S_ID = null;
      },


      HandleLogin:function(){
        var _this = this;
        if(Number(this.S_ID)<1){
          MessageBox("账号不能为空");
        }
        if(Number(this.S_ID)==0&&Number(this.password)>0){
          MessageBox('请先输入账号');
        }else{
          console.log('获取数据库');
          axios.get('http://23.83.244.88:8080/student')
            .then(function (response) {
              console.log(response.data);
              _this.my = response.data;
              console.log("数据response的长度是：",response.data.length);
              for (var i = 0;i<response.data.length;i++){
                // console.log(i);
                // console.log(response.data[3].number);
                // console.log(response.data[3].password);
                // console.log(_this.S_ID);
                if (Number(_this.S_ID)==response.data[i].number){
                  console.log("账号正确");
                  if(Number(_this.password)==response.data[i].password){
                    console.log("登陆成功");
                    // this.$emit('enlarge-text', response.data[i].name)
                    _this.$router.push('/TeaHome');

                    console.log(response.data[i].number);
                    console.log(response.data[i].password);
                    break;
                  }else{
                    MessageBox('密码错误');
                    break;
                  }
                } else{
                    if(i == response.data.length-1){
                      MessageBox('这个账号没有注册');
                    }
                }
              }
            })
            .catch(function (error) {
              console.log(error);
            });
        }
      },
      LoginInfo: function() {
        axios.post('/api/student')
          .then(this.getLoginInfoSucc)
          .catch(err => {
            console.log(err);
          });
      },
      getLoginInfoSucc:function (res) {
          console.log(res)
      },
      // HandleLogin:function () {
      //
      //   Axios.post('http://23.83.244.88:8080/student', data)
      //     .then(res => {
      //       console.log(res.data)
      //       if (res.data.code === 0) {
      //         this.$message({
      //           showClose: true,
      //           message: '注册成功',
      //           type: 'success'
      //         })
      //         router.push({name: 'Login'})
      //       } else {
      //         this.$message({
      //           showClose: true,
      //           message: '注册失败',
      //           type: 'error'
      //         })
      //       }
      //     })
      // }
    },
    mounted () {
      this.LoginInfo();
    }
  }
</script>

<style scoped>
  .set-button1{
    margin-top: 1rem;
    margin-left: 0rem;
    margin-right: 6.5rem;

  }
  .set-button1{
    margin-top: 1rem;
    margin-right: 3rem;
  }
</style>
